package org.wxy.sdk.base.impl;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;

import org.wxy.sdk.R;
import org.wxy.sdk.base.IBasePresenter;
import org.wxy.sdk.base.IBaseView;
import org.wxy.sdk.util.AppLog;
import org.wxy.sdk.util.DialogFactory;

/**
 * Created by wxy on 2018/05/27.
 *
 * @author wxy
 * @date on 2018/05/27 11:32
 */
class BaseViewImpl implements IBaseView {
    private Context context;

    private Toast toast;

    private String name;

    BaseViewImpl(Context context, String name) {
        this.context = context;
        this.name = name;
    }

    @Override
    public void showToast(String tip) {
        if (toast != null) {
            toast.cancel();
        }
        toast = Toast.makeText(context, tip, Toast.LENGTH_SHORT);
        toast.show();
    }

    @Override
    public void showToast(@StringRes int strResId) {
        showToast(context.getString(strResId));
    }

    @Override
    public void showLoadingDialog(@StringRes int strResId) {
        showLoadingDialog(context.getString(strResId));
    }

    @Override
    public void showLoadingDialog() {
        showLoadingDialog(R.string.loading);
    }

    @Override
    public void showLoadingDialog(String msg) {
        DialogFactory.getInstance().showProgressDialog(context, msg);
    }

    @Override
    public void hideLoadingDialog() {
        DialogFactory.getInstance().hideProgressDialog();
    }

    @Override
    public void showError(String message) {
       showError(context.getString(R.string.toast_error), message);
    }

    @Override
    public void showError(String title, String message) {
        DialogFactory.getInstance().showErrorDialog(context, title, message);
    }

    @Override
    public void showError(@StringRes int strResId) {
        showError(getContext().getString(strResId));
    }

    @Override
    public void showError(Throwable throwable, String message) {
        showError(message);
        AppLog.e(message, throwable);
    }

    @Override
    public void refresh() {
    }

    @Override
    public void destroy() {
        Log.i("BaseViewImpl", name + "->baseViewImpl:destroy");
        context = null;
        toast = null;
        name = null;
    }

    @Override
    public void navigation(String path) {
        navigation(path, null);
    }

    @Override
    public void navigation(String path, Bundle bundle) {
        ARouter.getInstance().build(path).with(bundle).navigation();
    }

    @Override
    public void navigation(String path, int requestCode) {
        ARouter.getInstance().build(path).navigation((Activity) getContext(), requestCode);
    }

    @Override
    public void navigation(String path, Bundle bundle, int requestCode) {
        ARouter.getInstance().build(path).with(bundle).navigation((Activity) getContext(), requestCode);
    }

    @Override
    public String getStringRes(@StringRes int strResId, Object... objects) {
        return context.getString(strResId, objects);
    }

    @Override
    public Context getContext() {
        return context;
    }

    @Override
    public IBasePresenter getPresenter() {
        return null;
    }
}
